package com.QLM.QLMFile.action;

import com.QLM.QLMFile.Vo.Details;
import com.QLM.QLMFile.Vo.ResponseBean;
import com.QLM.QLMFile.Vo.Admin;
import com.QLM.QLMFile.service.AddService;
import com.QLM.QLMFile.service.DetailsService;
import com.QLM.QLMFile.service.Impl.LoginServiceImpl;
import com.QLM.QLMFile.service.LoginService;
import com.QLM.QLMFile.service.UserService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@CrossOrigin("*")
@RequestMapping("/api")
public class UserController {

    @Autowired(required = false)
    private LoginService loginService;
    @Autowired(required = false)
    private LoginServiceImpl loginServiceImpl;
    @Autowired
    private UserService userService;
    @Autowired
    private DetailsService detailsService;
    @Autowired
    private AddService addService;

    //正常访问login界面
    @RequestMapping("/login")
    public String login() {
        return "200";
    }

    //检查登录信息
    @RequestMapping(value = "/checkLogin", method = RequestMethod.POST)
    public ResponseBean checkLogin(@RequestBody Admin admin) {
        //调用Service方法
        Admin adminInfo = loginService.checkLogin(admin.getUsername(), admin.getUsername());
        //若有admin则添加到model里面并且跳转到成功界面
        System.out.println("adminInfo" + adminInfo);
        if (adminInfo != null) {

            return new ResponseBean(0, "登录成功", adminInfo);
        }
        return new ResponseBean(500, "登录失败", adminInfo);
    }

    //测试超链接跳转能否取得session值
    @RequestMapping("/anotherpage")
    public String hrefpage() {
        return "anotherpage";
    }


    @RequestMapping("/searchDetails")
    public ResponseBean changeDetailsSex(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit, @RequestParam Map n) {
        //得到数据
        IPage result = detailsService.findbydetailsid(page, limit, n);
        if (result.getTotal() > 0) {
            return new ResponseBean(0, "操作成功", result.getRecords());
        }
        return new ResponseBean(500, "操作失败", result.getRecords());
    }

    @RequestMapping("/addUser")
    public String addUser(Details details) {
        //得到数据
        detailsService.save(details);
        return "200";

    }
}
